//
//  CRCGroupFlagView.m
//  HuaRun2
//
//  Created by admin on 17/8/31.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "CRCGroupFlagView.h"

@implementation CRCGroupFlagView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.backgroundColor = kRandomColor;// RGBA(249, 249, 249, 1);

    [self addSubview:self.groupLab];
    [self addSubview:self.crcFlagView];
//    [self addSubview:self.crcFlagTopView];
    
    self.infoStr = @"CRC Group";
}

- (void)layoutInit {
    weakself(ws);
    
    [self.crcFlagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.groupLab.mas_right).offset(kMargin10);
        make.centerY.mas_equalTo(ws.groupLab);
        make.size.mas_equalTo(CGSizeMake(kMargin10 * 2, kMargin10 * 2));
    }];
        
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws);
        make.left.mas_equalTo(ws.groupLab.mas_left);
        make.right.mas_equalTo(ws.crcFlagView.mas_right);
        make.height.mas_equalTo(40);
    }];
}
- (void)setImageSize:(CGSize)imageSize {
    _imageSize = imageSize;
    
    weakself(ws);
    [self.crcFlagView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.groupLab.mas_right).offset(kMargin10);
        make.centerY.mas_equalTo(ws.groupLab);
        make.size.mas_equalTo(imageSize);
    }];
    
}

- (void)setInfoStr:(NSString *)infoStr {
    _infoStr = infoStr;
    _groupLab.text = infoStr;
    
    weakself(ws);
    CGFloat widht = [infoStr WithFontSize:12 width:0];
    
    [self.groupLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws);
        make.height.top.mas_equalTo(ws);
        make.width.mas_equalTo(widht);
    }];
}

- (UILabel *)groupLab {
    if (!_groupLab) {
        self.groupLab = [UILabel new];
        _groupLab.textAlignment = NSTextAlignmentLeft;
        _groupLab.font = kGeneralFontSize;
        _groupLab.textColor = [UIColor colorWithHex:@"3c3c3c"];
        _groupLab.text = @"备注";
    }
    return _groupLab;
}

- (UIButton *)crcFlagView {
    if (!_crcFlagView) {
        self.crcFlagView = [UIButton new];
        [_crcFlagView setBackgroundImage:[UIImage imageNamed:@"未选中"]
                                forState:(UIControlStateNormal)];
        [_crcFlagView setBackgroundImage:[UIImage imageNamed:@"私有备注"]
                                forState:(UIControlStateSelected)];
        
    }
    return _crcFlagView;
}

//- (UIButton *)crcFlagTopView {
//    if (!_crcFlagTopView) {
//        self.crcFlagTopView = [UIButton new];
//        _crcFlagTopView.backgroundColor = [UIColor clearColor];
//    }
//    return _crcFlagTopView;
//}
@end
